package com.wannengbang.cloudleader.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.a_main.MainActivity;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.SystemConfigBean;
import com.wannengbang.cloudleader.dialog.PrivacyDialog;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.utils.SPManager;


public class SplashActivity extends BaseActivity {
    private Handler handler;
    private Runnable runnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        requestSystemConfig();
        handler = new Handler();
        if (TextUtils.isEmpty(SPManager.getInstance().getProtocol())) {
            PrivacyDialog dialog = PrivacyDialog.newInstance();
            dialog.show(getSupportFragmentManager(), "dialog");
        } else {
            handler.postDelayed(runnable = new Runnable() {
                @Override
                public void run() {
                    if (!TextUtils.isEmpty(SPManager.getInstance().getToken()) && !TextUtils.isEmpty(SPManager.getInstance().getUserInfoJson())) {
                        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                    finish();
                }
            }, 1500);
        }
    }

    public void requestSystemConfig() {
        new HomePageModelImpl().requestSystemConfig(new DataCallBack<SystemConfigBean>() {
            @Override
            public void onSuccessful(SystemConfigBean bean) {
                SPManager.getInstance().saveSystemConfig(new Gson().toJson(bean));
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
